package com.guanyong.admin.controllers.api;

import com.guanyong.admin.handle.UserHandle;
import com.guanyong.model.FuncModel;
import com.guanyong.model.RoleModel;
import com.guanyong.model.UserBaseModel;
import com.guanyong.services.FuncService;
import com.guanyong.services.RoleService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.management.relation.Role;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by mxk on 2017/6/7.
 */
@RestController
@RequestMapping("/api/func/")
public class FuncApi {
    @Autowired
    FuncService funcService;

    @Autowired
    UserHandle userHandle;

    @Autowired
    RoleService roleService;



    @RequestMapping(value = "functree", method = RequestMethod.GET)
    ResultObject<List<FuncModel>> functree(HttpServletRequest request) {
        ResultObject<List<FuncModel>> result = new ResultObject<List<FuncModel>>();
        List<FuncModel> list=funcService.GetTreeModel();
        result.setResult(list);
        return result;
    }//updatestate

    @RequestMapping(value = "getuserfunctree", method = RequestMethod.GET)
    ResultObject<List<FuncModel>> getUserFunctree(HttpServletRequest request) {
        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        ResultObject<List<FuncModel>> result = new ResultObject<List<FuncModel>>();
        if (userBaseModel!=null){
            Integer roleId=userBaseModel.getfRole();
            RoleModel roleModel=roleService.GetRoleById(roleId);
            String funcIdlist=roleModel.getfFuncs();
            if (funcIdlist!=null && !funcIdlist.equals(""))
            {
                List<FuncModel> list=funcService.GetFuncModleByIdlist(funcIdlist);
                result.setResult(list);

            }
            else{
                result.setCode(ResultCode.ServiceError);
            }

        }
        else {
            result.setCode(ResultCode.NoLogin);
        }


//        List<FuncModel> list=funcService.GetTreeModel();
//        result.setResult(list);
        return result;
    }//updatestate

    @RequestMapping(value = "addfunc", method = RequestMethod.POST)
    ResultObject<Integer> addFunc(HttpServletRequest request,
                                  @RequestParam String  func_name,
                                  @RequestParam Integer func_parentid,
                                  @RequestParam String func_desc,
                                  @RequestParam String func_url,
                                  @RequestParam Integer func_show,
                                  @RequestParam String func_pic){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        FuncModel funcModel=new FuncModel();
        funcModel.setfName(func_name);
        funcModel.setfDesc(func_desc);
        funcModel.setfPic(func_pic);
        funcModel.setfUrl(func_url);
        funcModel.setfShow(func_show);
        funcModel.setfParentid(func_parentid);
        Integer res=funcService.AddFunc(funcModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }


    @RequestMapping(value = "updatefunc", method = RequestMethod.POST)
    ResultObject<Integer> updateFunc(HttpServletRequest request,
                                     @RequestParam Integer func_id,
                                     @RequestParam String  func_name,
                                     @RequestParam Integer func_parentid,
                                     @RequestParam String func_desc,
                                     @RequestParam String func_url,
                                     @RequestParam Integer func_show,
                                     @RequestParam String func_pic){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        FuncModel funcModel=new FuncModel();
        funcModel.setfId(func_id);
        funcModel.setfName(func_name);
        funcModel.setfDesc(func_desc);
        funcModel.setfPic(func_pic);
        funcModel.setfUrl(func_url);
        funcModel.setfShow(func_show);
        funcModel.setfParentid(func_parentid);

        Integer res=funcService.Update(funcModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }
}
